package com.qqs.util;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public final class ConvertUtil {
	// static Logger log=LogManager.getLogger(ConvertUtil.class);
	public static String Byte2HexString(byte[] data) {
		String strtemp = new String();
		int b = data.length;
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + "";
			strtemp = strtemp + stra;
		}
		return strtemp.trim();
	}

	public static String Byte2HexString2(byte[] data) {
		String strtemp = new String();
		int b = data.length;
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase();
			if (i < b - 1)
				stra = stra + "";
			strtemp = strtemp + stra;
		}
		return strtemp.trim();
	}

	public static String Byte2HexString(byte[] data, int b) {
		String strtemp = new String();
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + " ";
			strtemp = strtemp + stra;
		}
		return strtemp;
	}

	public static String Byte2HexString(Byte[] data, int b) {
		String strtemp = new String();
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + " ";
			strtemp = strtemp + stra;
		}
		return strtemp;
	}

	public static String Byte2HexString(char[] data, int b) {
		String strtemp = new String();
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + " ";
			strtemp = strtemp + stra;
		}
		return strtemp;
	}

	public static String Byte2HexString(char[] data) {
		String strtemp = new String();
		int b = data.length;
		// strtemp = "\n" + strtemp + "数据总长度： " + String.valueOf(b) + "\n";
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + " ";
			strtemp = strtemp + stra;
			// Log.v("hex", hex.toUpperCase());
			// Log.e("xuhao", String.valueOf(i));
		}
		return strtemp;
	}

	// byte[] to HexString "xxxxxxx"
	public static String Byte2HexString_ID(byte[] data, int b) {
		String strtemp = new String();
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase();
			strtemp = strtemp + stra;
			// Log.v("hex", hex.toUpperCase());
			// Log.e("xuhao", String.valueOf(i));
		}
		return strtemp;
	}

	// byte[] to HexString " ID: xxxxxxx "
	public static String Byte2HexString_ID(char[] data, int b) {
		String strtemp = new String();
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase();
			strtemp = strtemp + stra;
			// Log.v("hex", hex.toUpperCase());
			// Log.e("xuhao", String.valueOf(i));
		}
		return strtemp;
	}

	/**
	 * 字符串转换成十六进制值
	 * 
	 * @param bin
	 *            String 我们看到的要转换成十六进制的字符串
	 * @return
	 */
	public static String bin2hex(String bin) {
		char[] digital = "0123456789ABCDEF".toCharArray();
		byte[] bs = bin.getBytes();
		StringBuilder sb = new StringBuilder(bs.length * 2);

		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param hex
	 *            String 十六进制
	 * @return String 转换后的字符串
	 */
	public static String hex2bin(String hex) {
		String digital = "0123456789ABCDEF";
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return new String(bytes);
	}

	/**
	 * java字节码转字符串
	 * 
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) { // 一个字节的数，

		// 转成16进制字符串

		String hs = "";
		String tmp = "";
		for (int n = 0; n < b.length; n++) {
			// 整数转成十六进制表示

			tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (tmp.length() == 1) {
				hs = hs + "0" + tmp;
			} else {
				hs = hs + tmp;
			}
		}
		tmp = null;
		return hs.toUpperCase(); // 转成大写

	}

	/**
	 * 字符串转java字节码
	 * 
	 * @param b
	 * @return
	 */
	public static byte[] hex2byte(byte[] b) {
		if ((b.length % 2) != 0) {
			throw new IllegalArgumentException("长度不是偶数");
		}
		byte[] b2 = new byte[b.length / 2];
		for (int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			// 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个进制字节

			b2[n / 2] = (byte) Integer.parseInt(item, 16);
		}
		b = null;
		return b2;
	}

	// hex2char
	public static char[] hex2Char(String hexString) {
		if (hexString.equals("")) {
			return null;
		} else {
			if (!hexString.matches("0x[0-9a-fA-F]+")) {
				throw new IllegalArgumentException("参数应是0x开头的十六进制字符串");
			}
			int codePoint = Integer.parseInt(hexString.substring(2), 16);
			if ((codePoint < 0) || (codePoint > Character.MAX_CODE_POINT)) {
				throw new IllegalArgumentException(hexString + " hexString表示的字符范围溢出");
			}
			return Character.toChars(codePoint);
		}

	}

	public static char[] string2Char(String str) {
		if (str == null || str.equals(""))
			return null;
		str = str.replaceAll("\\s{1,}", " ");
		String[] ss = str.split(" ");
		char[] cs = new char[128];
		int i = 0;
		for (String s : ss) {
			int codePoint = Integer.parseInt(s, 16);
			if ((codePoint < 0) || (codePoint > Character.MAX_CODE_POINT)) {
				throw new IllegalArgumentException(s + " hexString表示的字符范围溢出");
			}
			cs[i] = Character.toChars(codePoint)[0];
			i++;
		}
		if (i == 0)
			return null;
		char[] ncs = Arrays.copyOf(cs, i);
		return ncs;
	}

	public static char[] string2Char2(String str) {
		if (str == null || str.equals(""))
			return null;
		if (str.trim().length() != 8)
			return null;
		String[] ss = new String[4];
		ss[0] = str.substring(0, 2);
		ss[1] = str.substring(2, 4);
		ss[2] = str.substring(4, 6);
		ss[3] = str.substring(6, 8);
		char[] cs = new char[128];
		int i = 0;
		for (String s : ss) {
			int codePoint = Integer.parseInt(s, 16);
			if ((codePoint < 0) || (codePoint > Character.MAX_CODE_POINT)) {
				throw new IllegalArgumentException(s + " hexString表示的字符范围溢出");
			}
			cs[i] = Character.toChars(codePoint)[0];
			i++;
		}
		if (i == 0)
			return null;
		char[] ncs = Arrays.copyOf(cs, i);
		return ncs;
	}

	// List去除重复
	public static List FilterRepeat(List list) {
		List newlist = new ArrayList();
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			Object obj = iter.next();// 定义一个Object变量去接收
			if (!newlist.contains(obj)) {// 判断是否包含
				newlist.add(obj);
			}
		}
		return newlist;
	}

	// 截取8位
	public static String FormatID(String strinformation, String key) {
		String strtmp;
		strtmp = strinformation;
		int nEnd = strinformation.lastIndexOf(key);
		int nSize = key.length();
		strtmp = strtmp.substring(nEnd + nSize, nEnd + nSize + 8);
		return strtmp;
	}

	// 16进制显示
	public static void printHexString(byte[] b) {
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			System.out.print(hex.toUpperCase() + " ");
			// Log.v("hex", hex.toUpperCase());
			// Log.e("xuhao", String.valueOf(i));
		}
	}

	//
	public static String printHexString(byte[] a, int b) {
		String strtemp = "\n" + "数据总长度： " + String.valueOf(b) + "\n\n";
		for (int i = 0; i < b; i++) {
			String hex = Integer.toHexString(a[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			String stra = hex.toUpperCase() + " ";
			strtemp = strtemp + stra;
			// Log.v("hex", hex.toUpperCase());
			// Log.e("xuhao", String.valueOf(i));
		}
		return strtemp;
	}

	//
	public static String int2str(int a) {
		String b;
		b = String.valueOf(a);
		return b;
	}

	public static void main(String[] args) {
		String str = "0199D009";
		System.out.println(str.substring(4, 6));
		String[] ss = new String[4];
		ss[0] = str.substring(0, 2);
		ss[1] = str.substring(3, 4);
		ss[2] = str.substring(5, 6);
		ss[3] = str.substring(7, 8);
		System.out.println(string2Char2("0199D009"));

	}

	public static String int2hex(int val) {
		int base = 16;
		int q = val, r;
		String result = "";
		while (q >= base) {
			r = q % base;
			q = q / base;
			if (r < 10)
				result = r + result;
			else
				result = String.valueOf((char) ('A' + r - 10)) + result;
		}

		if (q < 10)
			result = q + result;
		else
			result = String.valueOf((char) ('A' + q - 10)) + result;

		if (result.length() % 2 == 1)
			result = "0" + result;

		// insert space for every 2 symbols
		String rr = result.substring(0, 2);
		for (int i = 2; i < result.length(); i += 2)
			rr += " " + result.substring(i, i + 2);

		return rr;
	}

	public static String getIpFromSocketAddress(SocketAddress sa) {
		String s = sa.toString();
		return s.split(":")[0].substring(1);
	}
}
